// Package order_by_hot 1_2
// @Title order_by_hot
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/2 18:40
// @Create  2021/9/2 18:40
package order_by_hot

// AddTwoNumbers 两数相加
func AddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	tag := 0 // 代表当前要不要进位
	sumNode := &ListNode{0, nil}
	head := sumNode
	for l1 != nil && l2 != nil {
		res := l1.Val + l2.Val + tag
		if res < 10 {
			sumNode.Next = &ListNode{res, nil}
			sumNode = sumNode.Next
			tag = 0
		} else {
			sumNode.Next = &ListNode{res % 10, nil}
			sumNode = sumNode.Next
			tag = 1
		}
		l1 = l1.Next
		l2 = l2.Next
	}
	for l1 != nil {
		res := l1.Val + tag
		if res < 10 {
			sumNode.Next = &ListNode{res, nil}
			sumNode = sumNode.Next
			tag = 0
		} else {
			sumNode.Next = &ListNode{res % 10, nil}
			sumNode = sumNode.Next
			tag = 1
		}
		l1 = l1.Next
	}
	for l2 != nil {
		res := l2.Val + tag
		if res < 10 {
			sumNode.Next = &ListNode{res, nil}
			sumNode = sumNode.Next
			tag = 0
		} else {
			sumNode.Next = &ListNode{res % 10, nil}
			sumNode = sumNode.Next
			tag = 1
		}
		l2 = l2.Next
	}
	if tag == 1 {
		sumNode.Next = &ListNode{
			Val:  1,
			Next: nil,
		}
	}
	return head.Next
}
